﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Interface.Attendance;
using EzDesk.Utility.Extensions;
using EzDesk.Utility.Generic;
using EzDesk.Utility.TimerService;
using EzDesk.Utility.Json;


namespace EzDesk.Apps.ServiceImpl.Attendance
{
    public partial class AttendanceService : ContainerAccessorBase, IAttendanceService
    {
        class DailyAttendanceReportTask : EverydayTimerTask
        {
            private readonly AttendanceService _attendanceService;

            public DailyAttendanceReportTask(TimeSpan dayExecuteTime, AttendanceService _attendanceService) 
                : base("DailyAttendanceReportTask", dayExecuteTime)
            {
                this.DayExecuteTime = dayExecuteTime;
                this._attendanceService = _attendanceService;
            }

            protected override void doExecute()
            {
                // 目标用户，所有参与考勤的员工 (所有人)
                var memberstring = "everyone";
                
                var attendanceDay = NextDay.AddDays(-1);

                var feedData = new Dictionary<string, string>
                                   {
                                       {"dateStr", attendanceDay.ToFullDate()},
                                       {"timeEpoch", attendanceDay.ToEpoch().ToString()},
                                       {"manHours", ""},
                                       {"workStatus", ""},
                                       {"evaluations", ""},
                                   };

                var feedId = _attendanceService._ezApiClient.SendFeed(
                    FeedTemplateStr_attendance_dailyAttendanceReport,
                    JsonHelper.ConvertToJson(feedData),
                    memberstring, isAvailable: false
                    );

                var targetUserList = _attendanceService._ezApiClient.GetSimpleUserInfos(memberstring);
                foreach (var user in targetUserList)
                {
                    var evaluation  = _attendanceService.GetDayAttendanceEvaluation(user.Id, attendanceDay).Result;
                    if (!evaluation.IsNeedToWork && evaluation.IsExtraWork)
                    {
                        // 如果不需要工作且没有加班则不发考勤统计
                        _attendanceService._ezApiClient.SetFeedIsAvailable(feedId, user.Id.ToString(), false);
                        continue;
                    }

                    var workStatusBuilder = new StringBuilder(" ");
                    if (evaluation.IsExtraWork)
                    {
                        workStatusBuilder.Append("(加班)");
                    }
                    if (evaluation.IsAbsense)
                    {
                        workStatusBuilder.Append("(旷工)");
                    }


                    var evaluationsBuilder = new StringBuilder(" ");
                    if (evaluation.IsLate)
                    {
                        evaluationsBuilder.AppendFormat("迟到 {0} 分钟<ez:br />", evaluation.LateMiniutes);
                    }
                    if (evaluation.IsLeaveEarly)
                    {
                        evaluationsBuilder.AppendFormat("早退 {0} 分钟<ez:br />", evaluation.EarlyMiniutes);
                    }
                    var feedDataEx = new Dictionary<string, string>
                                         {
                                             {"manHours", evaluation.Manhours.DivEx(10)},
                                             {"workStatus", workStatusBuilder.ToString()},
                                             {"evaluations", evaluationsBuilder.ToString()},
                                         };
                    _attendanceService._ezApiClient.UpdatePersonalFeedData(
                        feedId,
                        user.Id.ToString(),
                        JsonHelper.ConvertToJson(feedDataEx)
                        );
                }

                _attendanceService._ezApiClient.SetFeedIsAvailable(feedId, memberstring, true);
            }
        }
    }
}
